package com.bos.service.impl;

import com.bos.dao.repositories.TransitRepository;
import com.bos.dao.repositories.WayBillRepository;
import com.bos.domian.base.WayBill;
import com.bos.domian.transit.TransitInfo;
import com.bos.service.TransitService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author misterWei
 * @create 2019年01月26号:17点27分
 * @mailbox mynameisweiyan@gmail.com
 */
@Service
@Transactional(rollbackFor = {Exception.class})
public class TransitServiceImpl implements TransitService {
  @Autowired
  private WayBillRepository wayBillRepository;
  @Autowired
  private TransitRepository transitRepository;

    @Override
    public void transitJojnWayBillSave(String wayBillIds) {
      for (String wayBillId : wayBillIds.split(",")){
          //将这些运单与中转数据相关联，并且改变运单状态
          WayBill wayBill = wayBillRepository.getOne(Integer.parseInt(wayBillId));
          //创建中转记录数据
          TransitInfo transitInfoByWayBill =  transitRepository.findByWayBill(wayBill);
          if (transitInfoByWayBill == null){
              TransitInfo transitInfo = new TransitInfo();
              transitInfo.setWayBill(wayBill);
              transitInfo.setStatus("出入库中转");
              transitRepository.save(transitInfo);
              //change运单状态  派送中
              wayBill.setSignStatus(2);
          }else {
              throw new RuntimeException("运单已经转派！");
          }

      }
    }

    @Override
    public List<TransitInfo> findAll() {
        List<TransitInfo> all = transitRepository.findAll();
        return all;
    }
}
